/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * View_Bill.java
 *
 * Created on Sep 30, 2012, 8:25:15 PM
 */
package Bill;

import ConnnectData.DataConnect;
import entity.ViewCart;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import model.tableModelView_Bill;


/**
 *
 * @author HP
 */
public class View_Bill extends javax.swing.JFrame {

    /** Creates new form View_Bill */
    Connection con;
    Statement sta;
    ResultSet result;
    ResultSet result1;
    ResultSet result2;
    ResultSet result3;
    ResultSet result4;
    int staff_id1;
    int customer_id;
    int bill_Id;
    String Payment_Method;
    String Customer_name;
    String date;
    List<ViewCart> listCart1;
    tableModelView_Bill model1;
    private  int staf_id;

    public int getStaf_id() {
        return staf_id;
    }

    public void setStaf_id(int staf_id) {
        this.staf_id = staf_id;
    }
    public List<ViewCart> getListCart1() {
        return listCart1;
    }

    public void setListCart1(List<ViewCart> listCart1) {
        this.listCart1 = listCart1;
    }
    
    public View_Bill(List<ViewCart> list , int staf_id) {
        initComponents();
        
        
         if (list == null) {
            this.listCart1 = new ArrayList<ViewCart>();
        } else {
            this.listCart1 = list;
        }
         this.staf_id = staf_id;
        try {
            con = DataConnect.connectDB();
            sta = con.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.Load();
        
        lblValueBill_Id.setText("");
        lblValueCuatomer_ID.setText("");
        lblValuePayment.setText("");
        lblValueStaff_Id.setText("");
        lblValueTota.setText("");
        lblvalueCustomer_Name.setText("");
        lblvalueDate.setText("");
        
        try {
         staff_id1 = getStaf_id();
          System.out.println("id : " + staff_id1);
         
         result = sta.executeQuery("select MAX(Bill_Id)as  bill_id from Bill");
         while(result.next())
         {
             bill_Id = result.getInt("bill_id");
         }
           
         result1 = sta.executeQuery("select MAX(Customer_Id) as Customer_id from Customer");
         while(result1.next())
         {
             customer_id = result1.getInt("Customer_id");
         }
         
         result2 = sta.executeQuery("select top 1  Payment_Methods from Bill  order by Bill_Id desc");
         while(result2.next())
         {
             Payment_Method = result2.getString("Payment_Methods");
         }
         
         result3 = sta.executeQuery("select top 1  Customer_Name from Customer order by Customer_Id desc");
         while(result3.next())
         {
             Customer_name = result3.getString("Customer_Name");
         }
         
         result4 = sta.executeQuery("select top 1 convert(char(12), Create_Date,101)as Create_Date from Bill order by Bill_Id desc");
         while(result4.next())
         {
             date = result4.getString("Create_Date");
         }
         
         float tt = 0;
            for (int i = 0; i < listCart1.size(); i++) {
                float totalc = Float.valueOf(listCart1.get(i).getTotal());
                tt = totalc + tt;
            }
         
            lblValueBill_Id.setText(bill_Id+"");
            lblValueCuatomer_ID.setText(customer_id+"");
            lblValueStaff_Id.setText(staff_id1+"");
            lblValuePayment.setText(Payment_Method);
            lblValueTota.setText(tt+"");
            lblvalueCustomer_Name.setText(Customer_name);
            lblvalueDate.setText(date);
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
    
    
    public View_Bill()
    {
        initComponents();
        this.setLocationRelativeTo(null);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(View_Bill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(View_Bill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(View_Bill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(View_Bill.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //this.Load();
    }
    
     public void Load() {
        model1 = new tableModelView_Bill(listCart1);
        //model1.showList();
        tbViewBill.setModel(model1);
    }
    
//     public void View_bill1()
//     {
//         try {
//         staff_id = getStaf_id();
//         
//         result = sta.executeQuery("select MAX(Bill_Id)as  bill_id from Bill");
//         while(result.next())
//         {
//             bill_Id = result.getInt("bill_id");
//         }
//           
//         result1 = sta.executeQuery("select MAX(Customer_Id) as Customer_id from Customer");
//         while(result1.next())
//         {
//             customer_id = result1.getInt("Customer_id");
//         }
//         
//         float tt = 0;
//            for (int i = 0; i < listCart1.size(); i++) {
//                float totalc = Float.valueOf(listCart1.get(i).getTotal());
//                tt = totalc + tt;
//            }
//         
//            lblValueBill_Id.setText(bill_Id+"");
//            lblValueCuatomer_ID.setText(customer_id+"");
//            lblValueStaff_Id.setText(staff_id+"");
//            lblValueTota.setText(tt+"");
//         } catch (Exception e) {
//             e.printStackTrace();
//         }
//     }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        lblBill_Id = new javax.swing.JLabel();
        lblValueBill_Id = new javax.swing.JLabel();
        lblStaff_Id = new javax.swing.JLabel();
        lblValueStaff_Id = new javax.swing.JLabel();
        lblDatetime = new javax.swing.JLabel();
        lblvalueDate = new javax.swing.JLabel();
        lblCustomer_Id = new javax.swing.JLabel();
        lblValueCuatomer_ID = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblvalueCustomer_Name = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbViewBill = new javax.swing.JTable();
        lbPayment = new javax.swing.JLabel();
        lblValuePayment = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        lblValueTota = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jInternalFrame1.setTitle("View Bill");
        jInternalFrame1.setVisible(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/View_Bill.png"))); // NOI18N

        lblBill_Id.setForeground(new java.awt.Color(255, 0, 0));
        lblBill_Id.setText("Bill_Id :");

        lblValueBill_Id.setText("jLabel3");

        lblStaff_Id.setForeground(new java.awt.Color(255, 0, 0));
        lblStaff_Id.setText("Staff_Id :");

        lblValueStaff_Id.setText("jLabel3");

        lblDatetime.setForeground(new java.awt.Color(255, 0, 51));
        lblDatetime.setText("Date :");

        lblvalueDate.setText("jLabel2");

        lblCustomer_Id.setForeground(new java.awt.Color(255, 0, 0));
        lblCustomer_Id.setText("Customer_Id :");

        lblValueCuatomer_ID.setText("jLabel3");

        jLabel2.setForeground(new java.awt.Color(255, 0, 51));
        jLabel2.setText("Customer_Name :");

        lblvalueCustomer_Name.setText("jLabel3");

        tbViewBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbViewBill);

        lbPayment.setForeground(new java.awt.Color(255, 0, 0));
        lbPayment.setText("Payment_Method :");

        lblValuePayment.setText("jLabel4");

        lblTotal.setForeground(new java.awt.Color(255, 0, 0));
        lblTotal.setText("Total :");

        lblValueTota.setText("jLabel4");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(204, 0, 0));
        jLabel3.setText("$");

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(lblBill_Id)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblValueBill_Id))
                    .addComponent(lblCustomer_Id))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblValueCuatomer_ID)
                .addGap(90, 90, 90)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(lblvalueCustomer_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(lblStaff_Id)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblValueStaff_Id)
                        .addGap(114, 114, 114)
                        .addComponent(lblDatetime)
                        .addGap(18, 18, 18)
                        .addComponent(lblvalueDate)))
                .addContainerGap(122, Short.MAX_VALUE))
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbPayment)
                .addGap(18, 18, 18)
                .addComponent(lblValuePayment)
                .addGap(90, 90, 90)
                .addComponent(lblTotal)
                .addGap(18, 18, 18)
                .addComponent(lblValueTota)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addContainerGap(288, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap(201, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(196, 196, 196))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(43, 43, 43)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBill_Id)
                    .addComponent(lblStaff_Id)
                    .addComponent(lblvalueDate)
                    .addComponent(lblDatetime)
                    .addComponent(lblValueStaff_Id)
                    .addComponent(lblValueBill_Id))
                .addGap(28, 28, 28)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCustomer_Id)
                    .addComponent(lblValueCuatomer_ID)
                    .addComponent(jLabel2)
                    .addComponent(lblvalueCustomer_Name))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPayment)
                    .addComponent(lblValuePayment)
                    .addComponent(jLabel3)
                    .addComponent(lblValueTota)
                    .addComponent(lblTotal))
                .addContainerGap(169, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(View_Bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(View_Bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(View_Bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(View_Bill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new View_Bill().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbPayment;
    private javax.swing.JLabel lblBill_Id;
    private javax.swing.JLabel lblCustomer_Id;
    private javax.swing.JLabel lblDatetime;
    private javax.swing.JLabel lblStaff_Id;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblValueBill_Id;
    private javax.swing.JLabel lblValueCuatomer_ID;
    private javax.swing.JLabel lblValuePayment;
    private javax.swing.JLabel lblValueStaff_Id;
    private javax.swing.JLabel lblValueTota;
    private javax.swing.JLabel lblvalueCustomer_Name;
    private javax.swing.JLabel lblvalueDate;
    private javax.swing.JTable tbViewBill;
    // End of variables declaration//GEN-END:variables
}
